﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RuiXinCoffee.DAL;
using RuiXinCoffee.Model;
using RuiXinCoffee.Utils;

//后续待完善
namespace RuiXinCoffee.BLL
{
    internal class OrderService
    {
        private OrderDAO orderDAO = new OrderDAO();
        private ProductDAO productDAO = new ProductDAO();
        
        //创建订单
        public bool CreateOrder(OrderInfo order)
        {
            try
            {
                //验证
                ValidateOrder(order);
                
                //总价
                order.TotalPrice = order.Price * order.Quantity;
                
                //默认状态为待付款
                order.OrderStatus = 0;
                
                int result = orderDAO.AddOrder(order);
                return result > 0;
            }
            catch (Exception ex)
            {
                throw new BusinessException("创建订单失败", ex);
            }
        }
        
        //获取所有订单
        public List<OrderInfo> GetAllOrders()
        {
            try
            {
                return orderDAO.GetAllOrders();
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取订单列表失败", ex);
            }
        }
        
        //根据订单状态获取订单
        public List<OrderInfo> GetOrdersByStatus(int status)
        {
            try
            {
                return orderDAO.GetOrdersByStatus(status);
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取订单列表失败", ex);
            }
        }
        
        //更新订单状态
        public bool UpdateOrderStatus(int orderId, int status)
        {
            try
            {
                int result = orderDAO.UpdateOrderStatus(orderId, status);
                return result > 0;
            }
            catch (Exception ex)
            {
                throw new DatabaseException("更新订单状态失败", ex);
            }
        }
        
        //删除订单
        public bool DeleteOrder(int orderId)
        {
            try
            {
                int result = orderDAO.DeleteOrder(orderId);
                return result > 0;
            }
            catch (Exception ex)
            {
                throw new DatabaseException("删除订单失败", ex);
            }
        }
        
        //获取订单详情
        public OrderInfo GetOrderById(int orderId)
        {
            try
            {
                return orderDAO.GetOrderById(orderId);
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取订单详情失败", ex);
            }
        }
       
        
        /*//暂时没考虑支付具体如何实现，先注释掉
        //完成付款
        public bool CompletePayment(int orderId)
        {
            try
            {
                return UpdateOrderStatus(orderId, 1); // 状态1表示已完成
            }
            catch (Exception ex)
            {
                throw new ApplicationException("完成付款失败：" + ex.Message);
            }
        }
        */
        
        //取消订单
        public bool CancelOrder(int orderId)
        {
            try
            {
                return UpdateOrderStatus(orderId, 2); //状态2表示已取消
            }
            catch (Exception ex)
            {
                throw new BusinessException("取消订单失败", ex);
            }
        }
        
        //验证订单是否合理
        private void ValidateOrder(OrderInfo order)
        {
            if (order.ProductId <= 0)
            {
                throw new UserOperationException("产品ID无效");
            }
            
            if (order.Quantity <= 0)
            {
                throw new UserOperationException("订单数量必须大于0");
            }
            
            if (order.Price <= 0)
            {
                throw new UserOperationException("产品价格必须大于0");
            }
            
            if (string.IsNullOrWhiteSpace(order.ProductName))
            {
                //尝试从数据库获取产品名称
                Product product = productDAO.GetProductById(order.ProductId);
                if (product != null)
                {
                    order.ProductName = product.Name;
                    order.ProductImageUrl = product.ImageUrl;
                    order.Price = product.Price;
                }
                else
                {
                    throw new BusinessException("无法获取产品信息");
                }
            }
        }
    }
}
